<?php

namespace extend;

class DataSet
{
    static function split($val,$is_insert=true,$field=[])
    {
        if($is_insert){
            $result=is_array($val)?implode(',',$val):$val;
        }else{
            $result=$val?array_filter(explode(',',$val)):[];
        }

        return $result;
    }

    static function json($val,$is_insert=true,$field=[])
    {
       if($is_insert){
            $result=$val?json_encode( $val,JSON_UNESCAPED_UNICODE):'';
        }else{
            $result=$val?json_decode($val,true):[];
        }
        return $result;
    }


    static function str2time($val,$is_insert=true,$field=[]){

        if($is_insert){
            $result=$val?strtotime($val):'';
        }else{
            $result=$val && $field['type']=='datetime' ? date('Y-m-d H:i:s',$val):$val;
        }
        return $result;
    }


    static function switch($val,$is_insert=true,$field=[]){

        if($is_insert){
            $result= $val == "false"?0:1;
        }else{
            $result=$val;
        }
        return $result;
    }
}